﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Drawing;

namespace Charities.ModelViews
{
    class MainSettingView
    {
        public virtual string Name { get; set; }
        public virtual string Address { get; set; }
        public virtual string AuthNumber { get; set; }
        public virtual string PhoneNumaber { get; set; }
        public virtual string Fax { get; set; }
        public virtual string Email { get; set; }
        public virtual string LogoPath { get; set; }
        public virtual Image LogoImage { get; set; }

        public void SaveNewMainSetting(string path)
        {
            XElement settings =new XElement("Root",
                      new XElement("Charity",
                          new XElement("Name", Name),
                          new XElement("AuthNumber", AuthNumber),
                          new XElement("Address", Address),
                          new XElement("PhoneNumaber", PhoneNumaber),
                          new XElement("LogoPath", LogoPath),
                          new XElement("Fax", Fax),
                          new XElement("Email", Email)));
            settings.Save(path);
        }
        public void UpdateSetting(string path)
        {
            XElement DOC = XElement.Load(path);
            var setting = DOC.Elements("Charity").FirstOrDefault();
            setting.SetElementValue("Name", Name);
            setting.SetElementValue("AuthNumber", AuthNumber);
            setting.SetElementValue("Address", Address);
            setting.SetElementValue("PhoneNumaber", PhoneNumaber);
            setting.SetElementValue("LogoPath", LogoPath);
            setting.SetElementValue("Fax", Fax);
            setting.SetElementValue("Email", Email);
            DOC.Save(path);
        }
        public MainSettingView ReadSetting(string path)
        {
            XElement DOC = XElement.Load(path);
            var set = (from el in DOC.Elements("Charity")
                       select new MainSettingView
                       {
                           Name = el.Element("Name").Value,
                           Address = el.Element("Address").Value,
                           AuthNumber = el.Element("AuthNumber").Value,
                           PhoneNumaber = el.Element("PhoneNumaber").Value,
                           LogoPath = el.Element("LogoPath").Value,
                           Fax = el.Element("Fax").Value,
                           Email = el.Element("Email").Value,
                          
                       }).First();
           
            return set;
        }

    }
}
